home *** CD-ROM | disk | FTP | other *** search
- //=============================================================================
- // CodePlay.
- //=============================================================================
- class CodePlay extends Actor placeable;
-
- // #1 Data Members
- // This is like a class
- struct CodeStatus{
- var string Letter;
- var bool Found;
- var int Times;
- };
- // These are public
- const CODELENGTH = 8;
- const LOWASCII = 97;
- const HIGHASCII = 122;
- const NUMOFTRIES = 50;
-
- // #2 Use the struct type to declare an array
- var private CodeStatus AttemptedFinds[CODELENGTH];
-
- var private string CodeForSearch;
- var private string MessageAboutCode;
- var private string Report;
-
- //========= Interface of Code Class ================
-
- // #3 with PostBeginPlay, create the code
- function public MakeCode(){
- local int Ctr;
- while (Ctr < CODELENGTH){
- CodeForSearch $= Chr(GenerateRandom());
- Ctr++;
- }
- }
-
-
-
- // #4 Provide messages for Message property
- function public string ProvideCodeMessage(){
- RunCodeFinder();
- return CodeForSearch $ MessageAboutCode $ Report;
- }
-
-
- // Accessor
- function private string ShowCode(){
- return CodeForSearch;
- }
-
- //===================================================
-
- function private int GenerateRandom(){
- local float High, Low;
- Low = LOWASCII;
- High = HIGHASCII;
- return Int(RandRange(Low, High));
- }
-
- // #5 Central private function for the class
- function private RunCodeFinder(){
- SetGoals();
- DetectLetter();
- CheckForCompleteCode();
- CreateReport();
- }
-
-
-
- // #6 Uses Right() and Left() built-in functions
- // to sequentially retrieve letters and place
- // them in the Letter memeber of the array elements
- // Initialize all Founds with false
- // Initialize all Times to 0
- function private SetGoals(){
- local int Ctr;
- Ctr = 0;
- while(Ctr < CODELENGTH){
- AttemptedFinds[Ctr].Letter
- = Right( Left(CodeForSearch, Ctr+1), 1);
- AttemptedFinds[Ctr].Found = false;
- AttemptedFinds[Ctr].Times = 0;
- Ctr++;
- }
- }
-
-
- // #7 Gather the information on the status of each
- // Attempted discovery and assign it to Report
- function private CreateReport(){
- local int Ctr;
- Report="";
- while(Ctr < CODELENGTH){
- Report $= AttemptedFinds[Ctr].Letter
- $ AttemptedFinds[Ctr].Found
- $ AttemptedFinds[Ctr].Times;
- Ctr++;
- }
- }
-
- // #8 Embedded while statements with a selection statment
- // While the number of tries allowed
- // While each letter in the code string
- // Compare a randomly randomly generated letter
- // with the letter in the Letter member
- // If the letters match
- // set Found to True
- // increment Times
- function private DetectLetter(){
- local int Ctr, Itr;
- Ctr =0;
- while(Ctr < NUMOFTRIES){
- Itr = 0;
- while(Itr < CODELENGTH){
- if( AttemptedFinds[Itr].Letter == Chr(GenerateRandom() ) ){
- AttemptedFinds[Itr].Found = true;
- AttemptedFinds[Itr].Times++;
- }// end if
- Itr++;
- }// end inner while
- Ctr++;
- }// end outer while
- }// end Detect
-
- //#9 For the length of the code string
- // Find all the letters that have been found
- // If all letters have been found, show code complete
- // If all letters have not been found, say keep looking
- function private CheckForCompleteCode(){
- local int Itr, Goal;
- Itr = 0;
- Goal = 0;
- while(Itr < CODELENGTH){
- if(AttemptedFinds[Itr].Found == true){
- Goal++;
- if(Goal == CODELENGTH){
- MessageAboutCode = "Great! You found the complete code!" ;
- CodeForSearch="";
- MakeCode();
- SetGoals();
- break;
- }else{
- MessageAboutCode = "Still incomplete. Keep looking." ;
- }// end inner if else
- }// end outer if
- Itr++;
- }// end while
- }// end Check
-
-
-
-
-